package shopping.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shopping.demo.annotation.ApiIdempotent;
import shopping.demo.code.ResponseCode;
import shopping.demo.entities.response.Response;
import shopping.demo.entities.User;
import shopping.demo.service.UserService;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
//    @Autowired
//    private SecurityUtil securityUtil;
    private Logger logger = LoggerFactory.getLogger(Logger.class);

    //获取单个用户的信息，此处用于判断创建新用户时候用户名是否在数据库中已经存在
    @GetMapping("/user")
    @ResponseBody
    public String findByUserId(Long userId){
        JSONObject response = new JSONObject();
        logger.info("验证用户名中....");
        User user = userService.findByUserId(userId);
        logger.info("欢迎用户{}登录",user.getUsername());
        return response.toJSONString();
    }

    //获取所有用户信息
    @GetMapping("/list")
    public String findAll() {
        List<User> users = userService.findAll();
        return JSON.toJSONString(users.toString());
    }

    //用于添加新用户
    @PostMapping(value = "/user",consumes = "application/json")
    public String insertUser(@RequestBody JSONObject jsonObject) throws Exception {
        Long userId = jsonObject.getLong("userId");
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        Integer money = jsonObject.getInteger("money");

        userService.insertUser(new User(userId,username,password,money,"user"));
        return JSONObject.toJSONString(new Response(ResponseCode.USER_SCCUESS.getCode(),ResponseCode.USER_SCCUESS.getMsg()));
    }

    //用来修改密码
    @PatchMapping(value = "/password",consumes = "application/json")
    public String changePassword(@RequestBody JSONObject jsonObject) throws Exception {
        Long userId = jsonObject.getLong("userId");
        String password = jsonObject.getString("password");
        userService.changePassword(userId,password);
        return JSONObject.toJSONString(new Response(ResponseCode.USER_SCCUESS.getCode(),ResponseCode.USER_SCCUESS.getMsg()));
    }

    //增加用余额
    @ApiIdempotent
    @PatchMapping(value = "/money",consumes = "application/json")
    public String changeMoney(@RequestBody JSONObject jsonObject) throws Exception{
        Long userId = jsonObject.getLong("userId");

        userService.moneyUp(userId);
        return JSONObject.toJSONString(new Response(ResponseCode.USER_SCCUESS.getCode(),ResponseCode.USER_SCCUESS.getMsg()));
    }

    //删除用户
    @DeleteMapping(value = "/user",consumes = "application/json")
    public String deleteByUserId(@RequestBody JSONObject jsonObject) throws Exception{
        Long userId = jsonObject.getLong("userId");
        userService.deleteByUserId(userId);
        return JSONObject.toJSONString(new Response(ResponseCode.USER_SCCUESS.getCode(),ResponseCode.USER_SCCUESS.getMsg()));
    }
}
